

//   changed QSettings s(QSettings::Ini) to QSettings s("./cad.ini", QSettings::IniFormat);
#include "stdafx.h"
#include "pf_settings.h"

PF_Settings* PF_Settings::uniqueInstance = NULL;


PF_Settings::PF_Settings() {
    initialized = false;
    companyKey = "";
    appKey = "";
    group = "";
}

/**
 * Initialisation.
 *
 * @param companyKey String that identifies the company. Must start
 *        with a "/". E.g. "/RibbonSoft"
 * @param appKey String that identifies the application. Must start
 *        with a "/". E.g. "/LXCAD"
 */
void PF_Settings::init(const QString& companyKey,
                       const QString& appKey) {

    group = "";
	
    this->companyKey = companyKey;
    this->appKey = appKey;

    //insertSearchPath(QSettings::Windows, companyKey + appKey);
    //insertSearchPath(QSettings::Unix, "/usr/share/");
    initialized = true;
}


/**
 * Destructor
 */
PF_Settings::~PF_Settings() {
    while (!cache.isEmpty()) {
        QVariant *value = *cache.begin();
        cache.erase(cache.begin());
        delete value;
    }
}



void PF_Settings::beginGroup(const QString& group) {
    this->group = group;
}

void PF_Settings::endGroup() {
    this->group = "";
}

bool PF_Settings::writeEntry(const QString& key, int value) {
    return writeEntry(key, QVariant(value));
}

bool PF_Settings::writeEntry(const QString& key,const QString& value) {
    return writeEntry(key, QVariant(value));
}

bool PF_Settings::writeEntry(const QString& key, double value) {
    return writeEntry(key, QVariant(value));
}

bool PF_Settings::writeEntry(const QString& key, const QVariant& value) {
	QSettings s(companyKey, appKey);
    //   not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);

    s.setValue(QString("%1%2").arg(group).arg(key), value);

	addToCache(key, value);
	
	//   todo, remove bool 
    return true;
}

QString PF_Settings::readEntry(const QString& key,
                                 const QString& def,
                                 bool* ok) {
	
    // lookup:
    QVariant ret = readEntryCache(key);
    if (!ret.isValid()) {
				
        QSettings s(companyKey, appKey);
    	//   not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);
		
		if (ok!=NULL) {
			*ok=s.contains(QString("%1%2").arg(group).arg(key));
		}
		
        ret = s.value(QString("%1%2").arg(group).arg(key), QVariant(def));

		addToCache(key, ret);
    }

    return ret.toString();

}

QByteArray PF_Settings::readByteArrayEntry(const QString& key,
                    const QString& def,
                    bool* ok) {
    QVariant ret = readEntryCache(key);
    if (!ret.isValid()) {

        QSettings s(companyKey, appKey);
        //   not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);

                if (ok!=NULL) {
                        *ok=s.contains(QString("%1%2").arg(group).arg(key));
                }

        ret = s.value(QString("%1%2").arg(group).arg(key), QVariant(def));

                addToCache(key, ret);
    }

    return ret.toByteArray();

}

int PF_Settings::readNumEntry(const QString& key, int def, bool* ok) {

    // lookup:
    QVariant ret = readEntryCache(key);
    if (!ret.isValid()) {
        QSettings s(companyKey, appKey);
    	//   not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);

		if (ok!=NULL) {
			*ok=s.contains(QString("%1%2").arg(group).arg(key));
		}
        ret = s.value(QString("%1%2").arg(group).arg(key), QVariant(def));
		addToCache(key, ret);
	}
	return ret.toInt();
}


QVariant PF_Settings::readEntryCache(const QString& key) {
       QVariant *s = cache.value(key);
        if (s == NULL)
            return QVariant();
        else
            return *s;
}


void PF_Settings::addToCache(const QString& key, const QVariant& value) {
        delete cache.take(key);
        cache.insert(key, new QVariant(value));
}
